import { BaseModel } from './BaseModel.js';
import { dataFiles } from '../config/database.js';

const defaultGrades = [
  {
    id: 1,
    studentId: 1,
    studentName: '张三',
    studentNumber: '20240001',
    courseId: 1,
    courseCode: 'AI001',
    courseName: '企业级人工智能项目实训一',
    className: '计算机科学与技术1班',
    regularGrade: 85,
    midtermGrade: 78,
    finalGrade: 82,
    totalGrade: 82,
    gradePoint: 3.2,
    semester: '2023-2024-2',
    academicYear: '2023-2024',
    teachingMonth: '', // 教学月度
    examDate: '2024-01-15',
    teacher: '曲凤才',
    status: 'published',
    credit: 4,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 2,
    studentId: 2,
    studentName: '李四',
    studentNumber: '20240002',
    courseId: 2,
    courseCode: 'AI002',
    courseName: '企业级人工智能项目实训二',
    className: '软件工程2班',
    regularGrade: 90,
    midtermGrade: 85,
    finalGrade: 88,
    totalGrade: 88,
    gradePoint: 3.8,
    semester: '2023-2024-2',
    academicYear: '2023-2024',
    teachingMonth: '', // 教学月度
    examDate: '2024-01-15',
    teacher: '曲凤才',
    status: 'published',
    credit: 4,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 3,
    studentId: 1,
    studentName: '张三',
    studentNumber: '20240001',
    courseId: 3,
    courseCode: 'PY001',
    courseName: 'Python程序设计',
    className: '计算机科学与技术1班',
    regularGrade: 88,
    midtermGrade: 92,
    finalGrade: 90,
    totalGrade: 90,
    gradePoint: 4.0,
    semester: '2023-2024-2',
    academicYear: '2023-2024',
    examDate: '2024-01-20',
    teacher: '张老师',
    status: 'published',
    credit: 3,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  }
];

export class Grade extends BaseModel {
  constructor() {
    super(dataFiles.grades, defaultGrades);
  }

  findByStudentId(studentId) {
    const data = this.findAll();
    return data.filter(grade => grade.studentId === studentId);
  }

  findByStudentNumber(studentNumber) {
    const data = this.findAll();
    return data.filter(grade => grade.studentNumber === studentNumber);
  }

  findByCourseId(courseId) {
    const data = this.findAll();
    return data.filter(grade => grade.courseId === courseId);
  }

  findByCourseCode(courseCode) {
    const data = this.findAll();
    return data.filter(grade => grade.courseCode === courseCode);
  }

  findBySemester(semester) {
    const data = this.findAll();
    return data.filter(grade => grade.semester === semester);
  }

  findByStudentAndCourse(studentId, courseId) {
    return this.findOne({ studentId, courseId });
  }

  // 计算总成绩
  calculateTotalGrade(regularGrade, midtermGrade, finalGrade) {
    // 平时成绩30%，期中成绩30%，期末成绩40%
    return (regularGrade * 0.3) + (midtermGrade * 0.3) + (finalGrade * 0.4);
  }

  // 计算绩点
  calculateGradePoint(totalGrade) {
    if (totalGrade >= 90) return 4.0;
    if (totalGrade >= 85) return 3.7;
    if (totalGrade >= 82) return 3.3;
    if (totalGrade >= 78) return 3.0;
    if (totalGrade >= 75) return 2.7;
    if (totalGrade >= 72) return 2.3;
    if (totalGrade >= 68) return 2.0;
    if (totalGrade >= 64) return 1.5;
    if (totalGrade >= 60) return 1.0;
    return 0;
  }
}